package com.atguigu.tingshu.common.minio.service.impl;

import com.atguigu.tingshu.common.minio.config.MinioProperties;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.minio.service.FileUploadService;
import com.atguigu.tingshu.common.util.MD5;
import io.minio.*;
import io.minio.errors.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


/**
 * 文件上传接口实现类
 */
//@Service
@Slf4j
public class FileUploadServiceImpl implements FileUploadService {
    @Autowired
    private MinioProperties minioProperties;

    @Autowired
    private MinioClient minioClient;

    /**
     * 如何对两个内容相同的文件进行去重(MD5+文件的字节内容实现)
     * @param file
     * @return
     * @throws Exception
     */
    @Override
    public String fileUpload(MultipartFile file)  throws Exception{

        //不是对文件的名字去重,是对文件的内容去重
        //1.获取上传文件的文件名
        String picUrl = null;

        //minio中有一个api可以判断某个对象是否存在
        byte[] bytes = file.getBytes();
        String prefix = MD5.encrypt(new String(bytes));//文件内容的MD5值
        String suffix= file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."), file.getOriginalFilename().length());
        String objectName=prefix+suffix;
        //2.判断文件是否已经存在
        StatObjectArgs statObjectArgs = StatObjectArgs.builder().bucket(minioProperties.getBucketName()).object(objectName).build();
        try {
            //没有抛异常,这个文件已经存在
            minioClient.statObject(statObjectArgs);
            log.info("文件已经存在,不需要上传");
            picUrl=minioProperties.getEndpointUrl()+"/"+minioProperties.getBucketName()+"/"+objectName;
            return picUrl;
        } catch (Exception e) {
            //捕获异常,说明文件不存在,可以进行上传
            log.info("该文件对象不在指定tingshu桶中,是第一次上传不重复,需要进行上传");
        }

        try {

            //3.开始上传
            // Upload '/home/user/Photos/asiaphotos.zip' as object name 'asiaphotos-2015.zip' to bucket
            // 'asiatrip'.
            //uploadObject,需要文件的绝对路径
          /*  minioClient.uploadObject(
                    UploadObjectArgs.builder()
                            .bucket("demo") //桶名字
                            .object("myself.jpg") //对象名字
                            .filename("C:\\Users\\wqdqw\\Desktop\\share\\fig\\832934757edcd0f9a5f2a983e0200d40.jpg")
                            .build());*/
            //1.开始上传
            PutObjectArgs putObjectArgs = PutObjectArgs
                    .builder()
                    .bucket(minioProperties.getBucketName())
                    .object(objectName)
                    .stream(file.getInputStream(), file.getSize(), -1).build();
            minioClient.putObject(putObjectArgs);
            //2.需要自己拼接图片的url
            picUrl=minioProperties.getEndpointUrl()+"/"+minioProperties.getBucketName()+"/"+objectName;
            return picUrl;
        } catch (MinioException e) {
            log.info("Error occurred: {}", e.getMessage());
            throw new GuiguException(201,"上传失败");
        }
    }

}
